import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


class Base():
    # 初始化
    def __init__(self, driver):
        self.driver = driver

    # 浏览器
    def goto(self, url):
        self.driver.get(url)

    # 显示等待
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    # 点击
    def cilck(self, loc):
        self.find_element(loc).click()

    # 输入
    def send_keys(self, loc, info):
        return self.find_element(loc).send_keys(info)

    # 等待
    def text(self, loc):
        return self.find_element(loc).text

    # 滚动条（执行js代码）
    def gundong(self, x):
        self.driver.execute_script(f'window.scriptTo,(0,{x})')

    # 弹框处理
    def tanchuang(self):
        alert = self.driver.switch_to.alert()
        alert.accept()

    # iframe处理，
    def jinru(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))

    def tui(self):
        self.driver.switch_to.default_content()

    # 切换页面，
    def qiehuan(self, loc):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 鼠标事件，
    def shubiao(self, loc):
        ActionChains(self.driver).double_click(loc).perform()

    # 键盘事件
    def jianpan(self, loc):
        self.send_keys(loc, Keys.DELETE)

    # 浏览器事件，
    def liu(self):
        self.driver.refresh()

    # 下拉框选择
    def xiala(self, loc, x):
        v = Select(self.find_element(loc))
        v.select_by_index(x)
